<?php
/**
 * Created by PhpStorm.
 * Clerk: Administrator
 * Date: 2018/8/24
 * Time: 15:53
 */

namespace app\api\controller\v1;
use app\api\controller\BaseController;
use app\api\model\Banner;
use app\api\model\Client;
use app\api\model\Goods;
use app\api\model\Order;
use think\Session;

class Home extends BaseController
{

    /**
     * 首页
     * @return \think\response\Json
     */
    public function index(){
        $page = $this->request->get('page') ? $page = $this->request->get('page') : 1;
        $banner =  Banner::getBannerList();
        $goods_recommend = Goods::getGoodsRecommendList($page);

        $data = [
            'banner'=>$banner,
            'goods_recommend'=>$goods_recommend
        ];

        return json_success($data);
    }


    /**
     * 搜索
     * @return \think\response\Json
     */
    public function goodsSearch(){
        $page = $this->request->get('page') ? $page = $this->request->get('page') : 1;
        $goods = $this->request->get('goods');
        $goods_list = Goods::getGoodsList($page,$goods);
        return json_success($goods_list);
    }

    /**
     * 商品详情
     * @return \think\response\Json
     */
    public function goodsDetail(){
        $num = $this->request->get('num');
        $goods_id = $this->request->get('goods_id');
        $goods_detail = Goods::getGoodsDetail($goods_id);
        $token = $token = $this->request->header('token');
        if(Session::has($token)){
            $price = $goods_detail['price'];
        }else{
            $price = $goods_detail['offer_price'];
        }
        $data = [
            'name'=>$goods_detail['name'],
            'img'=>$goods_detail['img'],
            'price'=>$price*$num,
            'goods_content'=>$goods_detail['goods_content']
        ];

        return json_success($data);
    }

    /**
     * 立即购买
     */
    public function buyNow(){
        $token = $token = $this->request->header('token');
        if(Session::has($token)){
            //生成订单
            $client = $this->request->post('client');
            $addr = $this->request->post('addr');
            $phone = $this->request->post('phone');
            $money = $this->request->post('money');
            $num = $this->request->post('num');
            $goods_id = $this->request->post('goods_id');
            $clerk_id = Session::get($token);
            //添加联系人信息
            $data = [
                'address'=>$addr,
                'phone'=>$phone,
                'name'=>$client,
                'clerk_id'=>$clerk_id
            ];
            $res = Client::setClient($data);
            //订单数据
            $order_data =[
                'order_money'=>$money,
                'clerk_id'=>$clerk_id,
                'create_time'=>time()
            ];
            //商品订单数据
            $goods_data = [
                'goods_id'=>$goods_id,
                'goods_number'=>$num,
                'create_time'=>time(),
            ];
            if($res){
                //添加客户订单
                $order_nu = 'client'.date('YmdHis',time()).$clerk_id;
                $order_data['order_nu'] = $order_nu;
                $order_data['client_id'] = $res;
                $order_res = Order::setOrder($order_data,$goods_data,'client');
                if($order_res){
                    //订单详情
                    $data = Order::getOrder($order_res,$res);
                    return json_success($data);
                }else{
                    return json_failed(100004);
                }
            }else{
                //添加柜员订单
                $order_nu = 'clerk'.date('YmdHis',time()).$clerk_id;
                $order_data['order_nu'] = $order_nu;
                $order_res = Order::setOrder($order_data,$goods_data,'clerk');
                if($order_res){
                    //订单详情
                    $data = Order::getOrder($order_res);
                    return json_success($data);
                }else{
                    return json_failed(100004);
                }
            }

        }else{
            return json_failed(401);
        }

    }


}